package 代码记录.指定公司代码练习记录.华为_2024.分值100;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zx
 * @create 2024-02-02 19:59
 * 自己做出来了
 */
public class 内存冷热标记_18 {
    /**
     * map根据value排序变种：按照value由大到小输出key,如果key的value相等,再按照key由小到大输出
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0;i < n;i++){
            arr[i] = sc.nextInt();
        }
        int yuZhi = sc.nextInt();
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < arr.length;i++){
            map.put(arr[i], map.getOrDefault(arr[i],0) + 1);
        }
        List<Map.Entry<Integer, Integer>> entries = map.entrySet().stream().filter(
                entry -> entry.getValue() >= yuZhi
        ).collect(Collectors.toList());
        Collections.sort(entries, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                if(o1.getValue() == o2.getValue()){
                    return o1.getKey() - o2.getKey();//由小到大
                }else{
                    return o2.getValue() - o1.getValue();//由大到小
                }
            }
        });
        System.out.println(entries.size());
        if(entries.size() > 0){
            for (Map.Entry<Integer, Integer> entry : entries) {
                System.out.println(entry.getKey());
            }
        }

    }
}
